package com.audi.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.BaseActivity;
import com.audi.activity.HomeControlActivity;
import com.audi.activity.RootAccessActivity;
import com.audi.dto.PlaceItem;
import com.audi.message.ActionMessage;
import com.audi.message.SendMessage;
import com.audi.utils.CapabilityHelper;
import com.audi.view.BasePlaceDialog;

public class RootAccessAdapter extends BaseAdapter implements
		OnCheckedChangeListener {
	private Activity mActivity;
	private ArrayList<PlaceItem> mPlaceCardListInRootAccess;
	private BaseActivity mBaseActivity;
	@SuppressWarnings("unused")
	private int mIndexRootAccessList;

	public void setDataForAdapter(ArrayList<PlaceItem> placeCardListInRootAccess) {
		this.mPlaceCardListInRootAccess = placeCardListInRootAccess;
		this.notifyDataSetChanged();
	}
	public RootAccessAdapter(Activity activity,
			ArrayList<PlaceItem> placecradList) {
		this.mActivity = activity;
		this.mPlaceCardListInRootAccess = placecradList;
		this.mBaseActivity=(BaseActivity)activity;
		// mPlaceHandlerAction = new PlaceHandlerAction(mActivity);
	}
	@Override
	public int getCount() {
		return mPlaceCardListInRootAccess.size();
	}
	
	@Override
	public PlaceItem getItem(int index) {
		return mPlaceCardListInRootAccess.get(index);
	}

	@Override
	public long getItemId(int positionPlace) {
		return positionPlace;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup view) {
		final PlaceItem placeItem = mPlaceCardListInRootAccess.get(position);
		ViewHolder viewholder = null;
		// khoi tao cho viewholder
		if (convertView == null) {
			viewholder = new ViewHolder();
			convertView = LayoutInflater.from(mActivity).inflate(
					R.layout.row_root_access, null);
			viewholder.imageRoom = (ImageView) convertView
					.findViewById(R.id.img_room);
			viewholder.nameRoom = (TextView) convertView
					.findViewById(R.id.tv_name_room);
			viewholder.capability = (TextView) convertView
					.findViewById(R.id.tv_capability);
			convertView.setTag(viewholder);
			
			/*viewholder.btnMenuPlaceInRootAccess
					.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View view) {
							PopupMenu popupMenu = new PopupMenu(mActivity, view);
							popupMenu.getMenuInflater().inflate(
									R.menu.popup_menu_sub_root_access_place,
									popupMenu.getMenu());
							popupMenu
									.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
										public boolean onMenuItemClick(
												MenuItem item) {
											if (item.getItemId() == R.id.item_change_place) {
												((RootAccessActivity) mActivity)
												.changeCapabilityOfPlace(placeItem);
												
											}
											if (item.getItemId() == R.id.item_remove_this_place_root_access) {
												((RootAccessActivity) mActivity)
												.onRemovePlace(placeItem);
											}
											return true;
										}
									});
							popupMenu.show();
						}
					});*/
		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		// set data for listView
		if (TextUtils.isEmpty(placeItem.getPlaceImage())) {
			viewholder.imageRoom.setImageResource(R.drawable.icon_device_no_icon);
		} else {
			BitmapDrawable bm = new BitmapDrawable(mActivity.getResources(),
					placeItem.getPlaceImage());
			viewholder.imageRoom.setImageDrawable(bm);
		}

		viewholder.nameRoom.setText(placeItem.getPlaceName());
		String[] stringArray = mActivity.getResources().getStringArray(
				R.array.list_privacy_text);
		try {
			viewholder.capability
					.setText(stringArray[placeItem.getCapabilityType() - 1]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	
	@Override
	public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
	}

	

	
	static class ViewHolder {
		private ImageView imageRoom;
		private ImageButton btnMenuPlaceInRootAccess;
		private TextView nameRoom;
		public TextView capability;
	}
}